package com.news.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.news.service.usersService;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = -6859456401087824724L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=GBK");
		req.setCharacterEncoding("GBK");
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		
		usersService us=new usersService();
		int result=us.loginService(username, password);
		HttpSession session=req.getSession();
		if(result==1)
		{
			session.setAttribute("username", username);
			RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
			requestDispatcher.forward(req, resp);
		}
		else
		{
			req.setAttribute("username", "no");
			RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
			requestDispatcher.forward(req, resp);
		}
	}

}
